<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear='getOrdersList' @change='getOrdersList'>
            <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表区域 -->
      <el-table :data="orderslist" stripe border style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" min-width="180px">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" min-width="95px">
        </el-table-column>
        <el-table-column label="是否付款" min-width="70px">
          <template v-slot:default='scope'>
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否
        发货" min-width="70px">
        </el-table-column>
        <el-table-column label="下单时间" min-width="140px">
          <template v-slot:default='scope'>
            {{dateFormat(scope.row.create_time)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="140px">
          <template v-slot:default='scope'>
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showModifyDialog()"></el-button>
            <!-- 定位按钮 -->
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressDialog(scope.row.order_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 20, 30]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <!-- 修改地址区域 -->
      <el-dialog title="修改地址" :visible.sync="dialogModifyVisible" width="50%" @close="resetModifyForm">
        <el-form ref="modifyFormRef" :model="addressForm" :rules="rules" label-width="120px">
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader v-model="addressForm.address1" :options="cityData" :props="{ expandTrigger: 'hover'}" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogModifyVisible = false">取 消</el-button>
          <el-button @click="resetModifyForm">重置</el-button>
          <el-button type="primary" @click="dialogModifyVisible = false">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 物流信息区域 -->
      <el-dialog title="物流进度" :visible.sync="dialogProgressVisible" width="50%">
        <el-timeline>
          <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  name: 'MyOrder',
  data() {
    return {
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5
      },
      orderslist: [],
      total: 0,
      dialogModifyVisible: false,
      dialogProgressVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      cityData: cityData,
      progressInfo: [],
      rules: {
        address1: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    // 格式化时间
    dateFormat(ValDate) {
      const date = new Date(ValDate * 1000)
      const y = date.getFullYear()
      const m = this.addZero(date.getMonth() + 1)
      const d = this.addZero(date.getDate())
      const HH = this.addZero(date.getHours())
      const MM = this.addZero(date.getMinutes())
      const SS = this.addZero(date.getSeconds())
      return `${y}-${m}-${d} ${HH}:${MM}:${SS}`
    },
    // 为时间补零
    addZero(e) {
      return (e = e > 10 ? e : '0' + e)
    },
    async getOrdersList() {
      const { data: res } = await this.$http.get('/orders', { params: this.queryInfo })
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      this.orderslist = res.data.goods
      this.total = res.data.total
      // console.log(this.orderslist)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.queryInfo.pagesize = val
      this.getOrdersList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页在第 ${val} 页`)
      this.queryInfo.pagenum = val
      this.getOrdersList()
    },
    // 重置修改商品信息表单
    resetModifyForm() {
      this.$refs.modifyFormRef.resetFields()
    },
    // 显示修改地址表单
    showModifyDialog() {
      this.dialogModifyVisible = true
    },
    // 显示物流信息表单
    async showProgressDialog(orderId) {
      this.dialogProgressVisible = true
      // 注：下面的接口老师提供的已经无法使用，因此使用了评论区大佬的
      const { data: res } = await this.$http.get('http://120.78.137.246:8888/api/private/v1/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败！')
      }
      this.progressInfo = res.data
      // console.log(this.progressInfo)
    }
  }
}
</script>

<style lang='less' scoped>
.el-cascader {
  width: 100%;
}
</style>
